package com.cssl.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cssl.client.BookinformationClient;
import com.cssl.client.PublishedOpenFile;
import com.cssl.client.UserClient;
import com.cssl.pojo.*;
import com.cssl.service.BookinformationService;
import com.cssl.service.BooksmanagementService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
public class BooksmanagementController {
    /**
     * 查询逾期的数据
     */

    @Autowired
    private BooksmanagementService booksmanagementService;

    @Autowired
    private PublishedOpenFile publishedOpenFile;

    @Autowired
    private UserClient uclient;

    @Autowired
    private BookinformationClient bclient;

    @Autowired
    private BookinformationService bookinformationService;


    @RequestMapping("/load")
    public String load(int dqy,int size){
        Date date = new Date();
        Page<Booksmanagement> page = new Page<>(dqy,size);
        Page<Booksmanagement> page1 = booksmanagementService.page(page,new QueryWrapper<Booksmanagement>().lt("also",date));
        for (Booksmanagement boo:page.getRecords()) {
            //获取到逾期的天数
            long day =(date.getTime()-boo.getAlso().getTime())/(24*60*60*1000);
            Bookinformation info=bookinformationService.getOne(new QueryWrapper<Bookinformation>().eq("Callnumber",boo.getCallnumber()));
            Published publ= publishedOpenFile.selid(info.getPid());
            boo.setPublished(publ);
            boo.setAuthor(info.getAuthor());
            boo.setYuqi(day);
        }
        String str = JSON.toJSONStringWithDateFormat(page,"yyyy-MM-dd");
        return str;
    }

    @PostMapping("/uidall")
    public Booksmanagement loadjie(){
        Booksmanagement booksmanagement=new Booksmanagement();
        booksmanagement.setUsers(uclient.uidall());
        return booksmanagement;
    }

    @PostMapping("/bookall")
    public Booksmanagement bookall(){
        Booksmanagement booksmanagement=new Booksmanagement();
        List<Bookinformation> lists= bookinformationService.getBaseMapper().selectList(new QueryWrapper<Bookinformation>());
        booksmanagement.setBfs(lists);
        return  booksmanagement;
    }


    @PostMapping("/jieyue")
    public String jieyue(Integer dqy,Integer size){
        Page<Booksmanagement> page = new Page<>(dqy,size);
        Page<Booksmanagement> page1 = booksmanagementService.page(page,new QueryWrapper<Booksmanagement>().orderByDesc("jid"));
        String str = JSON.toJSONStringWithDateFormat(page,"yyyy-MM-dd");
        return str;
    }

    @GlobalTransactional
    @PostMapping("/insertjie")
    public boolean insertjie(Integer tid,String name){
       Bookinformation bf= bookinformationService.getBaseMapper().selectById(tid);
       Booksmanagement bm=new Booksmanagement();
       bm.setUname(name);
       bm.setSid(tid);
       bm.setCallnumber(bf.getCallnumber());
       bm.setBookname(bf.getTitle());
        Calendar cal = Calendar.getInstance();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //获取前面的时间用-负号
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH, +15);
        bm.setAlso(cal.getTime());
        Booklevel bl= bclient.selbname(bf.getBid());
        bm.setDailyrental(bl.getBmoney());
        if(booksmanagementService.getBaseMapper().insert(bm)>0){
            User uu= uclient.selectUser(name);
            System.out.println(uu);
            double m= bl.getBmoney()*15;
            uu.setMoney(uu.getMoney()-m);
            System.out.println(uu);
            if (uclient.updUser(uu)){
                History h=new History();
                h.setUid(uu.getRid());
                h.setName(name);
                h.setConsumption(m);
                h.setMoney(uu.getMoney());
                if(uclient.addHistory(h)){
                    return true;
                };
            }
        }
        return false;
    }

    @GlobalTransactional
    @PostMapping("/haishu")
    public  boolean haishu(Integer id, String time,double dayzujin,String name) throws ParseException {
        SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
        Date dd = s.parse(time);
        Date date = new Date();
        if (date.getTime()>dd.getTime()){
            long day =(date.getTime()-dd.getTime())/(24*60*60*1000);
            System.out.println("日子:"+day);
            double qian =day*dayzujin;
            User uu= uclient.selectUser(name);
            uu.setMoney(uu.getMoney()-qian);
            if (uclient.updUser(uu)){
                History h=new History();
                h.setUid(uu.getRid());
                h.setName(name);
                h.setConsumption(qian);
                h.setMoney(uu.getMoney());
                if(uclient.addHistory(h)){
                    if (booksmanagementService.getBaseMapper().deleteById(id)>0){
                        return true;
                    }
                };
            }
        }else{
            if (booksmanagementService.getBaseMapper().deleteById(id)>0){
                return true;
            }
        }
        return false;
    }
}
